﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace stuManage
{
    public partial class teaInf : System.Web.UI.Page
    {
        static List<Teacher> teachers = new List<Teacher>();
        DBHelper helper = new DBHelper();//用于执行数据库操作的对象
        /// <summary>
        /// 页面加载时候去完成的代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //1、定义存储学生信息的对象
            Teacher teacher = new Teacher();
            //2、获取信息
            teacher.ID = TextBox1.Text.Trim();
            teacher.Name = TextBox2.Text.Trim();
            teacher.Department = DropDownList1.SelectedValue;
            teacher.Age = Convert.ToInt32(TextBox3.Text.Trim());
            if (RadioButton1.Checked)//说明点的是RadioButton1
            {
                teacher.Sex = RadioButton1.Text;//输出RadioButton1.Text，即男
            }
            if (RadioButton2.Checked)//说明点的是RadioButton1
            {
                teacher.Sex = RadioButton2.Text;//输出RadioButton1.Text，即男
            }
            

            string commandString = "insert into teacher_inf values('{0}','{1}','{2}','{3}','{4}')";
            commandString = string.Format(commandString, teacher.ID, teacher.Name, teacher.Age, teacher.Sex, teacher.Department);
            helper.ExecuteSQL(commandString);

            //4、显示数据
            ShowTeachers("select * from teacher_inf");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
           

            //1、新建对象
            Teacher teacher = new Teacher();
            //2、获取信息
            teacher.ID = TextBox1.Text.Trim();

            //3、构造sql语句执行删除
            string commandString = "delete from teacher_inf where id='"+teacher.ID+"'";
            //commandString = string.Format(commandString, teacher.ID);
            helper.ExecuteSQL(commandString);
            DropDownList1.SelectedIndex = -1;
            TextBox1.Text = "";

            TextBox2.Text = "";
            TextBox3.Text = "";
            //4、显示数据
            ShowTeachers("select * from teacher_inf");
        }
        void ShowTeachers(string commandString)
        {
            //1、指定要执行的字符串
            SqlDataSource1.SelectCommand = commandString;   //指定查询的命令
            GridView1.DataSourceID = SqlDataSource1.ID;     //将数据源绑定到GridView上
            GridView1.DataBind();   //  绑定控件显示最后结果

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //1、定义存储学生信息的对象
            Teacher teacher = new Teacher();
            //2、获取信息
            teacher.ID = TextBox1.Text.Trim();
            teacher.Name = TextBox2.Text.Trim();
            teacher.Department = DropDownList1.SelectedValue;
            teacher.Age = Convert.ToInt32(TextBox3.Text.Trim());
            if (RadioButton1.Checked)//说明点的是RadioButton1
            {
                teacher.Sex = RadioButton1.Text;//输出RadioButton1.Text，即男
            }
            if (RadioButton2.Checked)//说明点的是RadioButton1
            {
                teacher.Sex = RadioButton2.Text;//输出RadioButton1.Text，即男
            }
            //3、构造sql语句执行删除
            string commandString = "update teacher_inf set name='{0}',age='{1}',sex='{2}',department='{3}' where id={4}";
            commandString = string.Format(commandString, teacher.Name, teacher.Age, teacher.Sex, teacher.Department, teacher.ID);
            helper.ExecuteSQL(commandString);
            //4、显示数据
            ShowTeachers("select * from teacher_inf");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            ////1、定义对象
            //Teacher teacher = new Teacher();
            ////2、收集信息
            //teacher.ID = TextBox1.Text.Trim();

            ////3、查询
            ////4、显示数据
            //ShowTeachers("select * from teacher_inf where id=" + teacher.ID);

            string id = TextBox1.Text.Trim();
            string name = TextBox2.Text.Trim();
            string department = DropDownList1.SelectedValue;
            if (id.Length > 0)
                ShowTeachers("select * from teacher_inf where id=" + id);
            else if (name.Length > 0)
                ShowTeachers("select * from teacher_inf where name='" + name + "'");
            else
                ShowTeachers("select * from teacher_inf where department='" + department + "'"); ;




        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            //4、显示数据
            ShowTeachers("select * from teacher_inf");

        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            DropDownList1.SelectedIndex = -1;
            TextBox2.Text = "";
            TextBox3.Text = "";

            string id = TextBox1.Text.Trim();
            string strConn = "server=localhost;port=3306;user id=root;password=lxy1756431734;database=student; pooling=true;";
            MySqlConnection conn = new MySqlConnection(strConn);
            conn.Open();//链接数据库  
            DataSet ds = new DataSet();
            string sql = "select * from teacher_inf where id = '" + id + "'";
            MySqlCommand com = new MySqlCommand(sql, conn);
            MySqlDataReader dr = com.ExecuteReader();
            if (dr.Read())
            {
                string name = dr["name"].ToString();
                string age = dr["age"].ToString();
                string sex = dr["sex"].ToString();
                string department = dr["department"].ToString();

                TextBox2.Text = name;
                TextBox3.Text = age;
                if (sex == "男")
                {
                    RadioButton2.Checked = false;
                    RadioButton1.Checked = true;

                }
                else
                {
                    RadioButton1.Checked = true;

                    RadioButton2.Checked = true;

                }
                DropDownList1.Items.FindByValue(department).Selected = true;


            }

        }
        protected void Button6_Click(object sender, EventArgs e)
        {
            //清除文本框内所有数据
            DropDownList1.SelectedIndex = -1;
            TextBox1.Text = "";

            TextBox2.Text = "";
            TextBox3.Text = "";
            ShowTeachers("select * from teacher_inf");


        }


    }
}
